{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f0dd9f04-4d8a-440f-a1e2-deb7152143e2",
   "metadata": {},
   "source": [
    "info from"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a3b06316-bff4-4c44-806d-d77e29454db4",
   "metadata": {},
   "source": [
    "https://blog.csdn.net/zhh_920509/article/details/129757516"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "772670aa-b5ea-4b29-8628-b03d3f087c97",
   "metadata": {},
   "source": [
    "AKShare Introduce"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fe2d76ae-a8e8-4c64-9362-62fdbcf0af7e",
   "metadata": {},
   "source": [
    "Gain stock data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "47de5f25-0812-4f1f-be61-219ef23314e1",
   "metadata": {},
   "outputs": [],
   "source": [
    "获取股票数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b1bcd19c-394a-4c37-a345-389b5f2672e4",
   "metadata": {},
   "outputs": [],
   "source": [
    "# General picture of the stock market\n",
    "import akshare as ak\n",
    "\n",
    "# Shanghai Stock Exchange\n",
    "# http://www.sse.com.cn/market/stockdata/statistic/\n",
    "def sh_df():\n",
    "    stock_sse_summary_df = ak.stock_sse_summary()\n",
    "    print(stock_sse_summary_df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "ab4db37e-8f0d-49c8-bf43-2e27d947b020",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Shenzhen Stock Exchange\n",
    "# 证券类别统计\n",
    "# http://www.szse.cn/market/overview/index.html\n",
    "def sz_df():\n",
    "    stock_szse_summary_df = ak.stock_szse_summary()\n",
    "    print(stock_szse_summary_df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "c02bd4df-51dd-4654-9eee-6b59ee729f0c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Shenzhen Stock Exchange\n",
    "# 地区trade sort\n",
    "# http://www.szse.cn/market/overview/index.html\n",
    "def sz_area():\n",
    "    stock_szse_area_summary_df = ak.stock_szse_area_summary(date='202203')\n",
    "    print(stock_szse_area_summary_df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "d92d1876-1d35-4773-8d66-4159f495b8f2",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "# 深圳证券交易所\n",
    "# 股票行业成交\n",
    "# http://docs.static.szse.cn/www/market/periodical/month/W020220511355248518608.html\n",
    "def sz_sector():\n",
    "    stock_szse_sector_summary_df = ak.stock_szse_sector_summary(symbol=\"当年\", date=\"202204\")\n",
    "    print(stock_szse_sector_summary_df)\n",
    " \n",
    "# 上海证券交易所\n",
    "# 每日概况\n",
    "#  http://www.sse.com.cn/market/stockdata/overview/day/\n",
    "def sh_day():\n",
    "    stock_sse_deal_daily_df = ak.stock_sse_deal_daily(date=\"20201111\")\n",
    "    print(stock_sse_deal_daily_df)\n",
    " \n",
    " \n",
    "def get_account_statistics():\n",
    "    # 股票账户统计月度\n",
    "    \"\"\"\n",
    "    输出参数\n",
    "    名称    类型    描述\n",
    "    数据日期    object    -\n",
    "    新增投资者-数量    float64    注意单位: 万户\n",
    "    新增投资者-环比    float64    -\n",
    "    新增投资者-同比    float64    -\n",
    "    期末投资者-总量    float64    注意单位: 万户\n",
    "    期末投资者-A股账户    float64    注意单位: 万户\n",
    "    期末投资者-B股账户    float64    注意单位: 万户\n",
    "    沪深总市值    float64    -\n",
    "    沪深户均市值    float64    注意单位: 万\n",
    "    上证指数-收盘    float64    -\n",
    "    上证指数-涨跌幅    float64    -\n",
    "    \"\"\"\n",
    "    account = ak.stock_account_statistics_em()\n",
    "    account.set_index(\"数据日期\", inplace=True)  # 设置索引值\n",
    "    account.to_csv(\"stock_account_statistics.csv\")\n",
    "    print(account)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "7ae4e546-fc5e-464a-8197-01c1b8e2f80d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "         新增投资者-数量  新增投资者-环比  新增投资者-同比  期末投资者-总量  期末投资者-A股账户  期末投资者-B股账户  \\\n",
      "数据日期                                                                      \n",
      "2023-08     99.59    0.0940   -0.2007  22141.58    22082.23      238.81   \n",
      "2023-07     91.03   -0.0724   -0.1959  22042.00    21982.61      238.86   \n",
      "2023-06     98.13    0.0089   -0.2623  21950.97    21891.53      238.91   \n",
      "2023-05     97.26   -0.0292   -0.1913  21852.84    21793.33      238.96   \n",
      "2023-04    100.19   -0.4716   -0.2028  21755.58    21696.01      238.99   \n",
      "...           ...       ...       ...       ...         ...         ...   \n",
      "2015-08    136.85   -0.3320       NaN   9406.60     9331.30      239.43   \n",
      "2015-07    204.87   -0.5587       NaN   9269.75     9194.35      239.22   \n",
      "2015-06    464.22    0.1163       NaN   9064.88     8989.29      238.83   \n",
      "2015-05    415.87   -0.1641       NaN   8600.66     8524.63      237.71   \n",
      "2015-04    497.53       NaN       NaN   8184.79     8108.30      235.92   \n",
      "\n",
      "                 沪深总市值   沪深户均市值    上证指数-收盘  上证指数-涨跌幅  \n",
      "数据日期                                                  \n",
      "2023-08  808664.768312  36.6206  3119.8764   -5.2009  \n",
      "2023-07  850616.505464  38.6950  3291.0398    2.7788  \n",
      "2023-06  832614.739654  38.0336  3202.0623   -0.0781  \n",
      "2023-05  825176.131820  37.8637  3204.5644   -3.5721  \n",
      "2023-04  849540.136222  39.1565  3323.2746    1.5404  \n",
      "...                ...      ...        ...       ...  \n",
      "2015-08  438027.184176  46.9417  3205.9855  -12.4938  \n",
      "2015-07  504845.638889  54.9082  3663.7256  -14.3433  \n",
      "2015-06  584573.654037  65.0300  4277.2219   -7.2537  \n",
      "2015-05  627465.456629  73.6062  4611.7440    3.8294  \n",
      "2015-04  563491.335038  69.4956  4441.6550   18.5105  \n",
      "\n",
      "[101 rows x 10 columns]\n"
     ]
    }
   ],
   "source": [
    "\n",
    "    # sh_df()\n",
    "    # sz_df()\n",
    "    # sz_area()\n",
    "    # sz_sector()\n",
    "    # sh_day()\n",
    "    get_account_statistics()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ec76395a-805f-4055-b88a-1ab5b6888ad5",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
